package online.szlib.com.rest.controller;

import java.util.List;

import online.szlib.com.common.utils.JSONHelper;
import online.szlib.com.core.domain.CartItem;
import online.szlib.com.core.services.CartService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@Controller
@RequestMapping("/api/cartitems")
public class CartItemRestController {

    private static Logger logger = LoggerFactory.getLogger(CartItemRestController.class);

    @Autowired
    private CartService cartService;

	// Http Get All
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<CartItem> getAll() {
		System.out.println("List CartItems >>>>>>>>>>>>>>>>>>>>");
		return cartService.listCartItems();
	}
	
	// Http Get One
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public CartItem get(@PathVariable("id") Long id) {
		logger.debug("Get One CartItem >>>>>>>>>>>>>>>>>>>>");
		return cartService.getCartItem(id);
	}
    
	// Http Create One
	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<CartItem> createCartItem(@RequestBody CartItem comment) {
		logger.info("Create one CartItem >>>>>>>>>>>>>>>>>>>>");
		cartService.create(comment);
		return new ResponseEntity<CartItem>(comment, JSONHelper.getJSONHeaders(), HttpStatus.CREATED);
	}
    
	// Http Update One CartItem
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	@ResponseStatus(HttpStatus.OK)
	public void update(@RequestBody CartItem entity, @PathVariable("id") Long id) {
		entity.setCartitemId(id);
		logger.info("Update one CartItem >>>>>>>>>>>>>>>>>>>>");
		cartService.update(entity);
	}
    
    // Http Delete One
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<Long> deleteCartItem(@PathVariable Long id) {
    	logger.debug("Delete One CartItem >>>>>>>>>>>>>>>>>>>>");
    	cartService.deleteById(id);
        return new ResponseEntity<Long>(id, HttpStatus.OK);
    }

}
